-- act命令处理中心

local mis_uu = require('modules.commhm.utils')
local mis_bit = require('bit')
local mis_timer = require('modules.commhm.ngxTimerWrap')
local log       = require("modules.commhm.log");
local uu = mis_uu
local ngx = ngx
local pairs = pairs
local next = next
local math_floor = math.floor
local math_ceil = math.ceil
local math_max = math.max
local math_min = math.min
local math_random = math.random
local math_randomseed = math.randomseed
local math_huge = math.huge
local string_match = string.match
local string_format = string.format
local os_date = os.date
local bor = mis_bit.bor
local band = mis_bit.band
local bnot = mis_bit.bnot
local lshift = mis_bit.lshift
local tonumber = tonumber
local tostring = tostring

local table_insert = table.insert
local table_remove = table.remove
local OVERDUE_SEC = 30*86400

local mis_config = nil
local mis_data = nil
local mis_log = nil
local mis_bridge = nil
local mis_common = nil
local  mis_extend = nil
local mis_allowIp = nil

local t_retCode = nil



--------------------------------------------------------------
local mis_act = {
	s_name = string.match(debug.getinfo(1).short_src, '.*/(.*).lua');	-- 模块名称
}
local this = mis_act

--------------------------------------------------------------
-- 说明：模块包含
-- 参数：模块表
this.modules_include = function(t_moddules)
	mis_config = t_moddules.mis_config
	mis_data = t_moddules.mis_data
	mis_log = t_moddules.mis_log
	mis_bridge = t_moddules.mis_bridge
	mis_common = t_moddules.mis_common
	mis_extend = t_moddules.mis_extend
	mis_allowIp = t_moddules.mis_allowIp
	t_retCode = mis_config.t_retCode

end


-- !@ 获得任务积分
local get_task_score = function( uin )
	local t_user = mis_data.get_user(uin)
	local n_code, t_spData = t_user:get_specialItem_data()
	if n_code ~= t_retCode.n_OK then
		return 0
	end

	return t_spData[mis_config.t_specialItem.s_THEMESCORE] or 0
end


local init_mission = function(t_userData, s_type)
	local t_vip_theme_Conf 	= mis_data.t_share_mission.vip_theme.t_conf
	if t_vip_theme_Conf == nil then
		return false, 'init_mission lack config! ---1'
	end
	local n_nowtime = mis_data.get_server_time()
	local t_taskConf 		= uu.table_value_copy(mis_data.t_share_mission[s_type])
	if not(t_taskConf and t_taskConf.t_conf and t_taskConf.t_conf.t_mission) then
		return false,  "init_mission lack config!--- 2"
	end

	local t_temp = t_taskConf.t_conf
	local n_mode = t_taskConf.t_conf.b_auto and 1 or 0
	for _, v in next, t_temp.t_mission do
		for _, v2 in next, v do
			t_userData.tasks = t_userData.tasks or {}
			t_userData.tasks[s_type] = t_userData.tasks[s_type] or {}
			t_userData.tasks[s_type][v2.n_subTaskId] = t_userData.tasks[s_type][v2.n_subTaskId] or { progress = 0, status = 0, event = v2.n_eventId, refreshTime = n_nowtime, rewardTime = 0, mode = n_mode, total = 0 }
		end
	end
	return true
end


local get_task_data = function(t_userData, s_type, n_subTaskId)
	local bneed_init = false
	local n_nowtime = mis_data.get_server_time()
	if not t_userData.tasks or not  t_userData.tasks[s_type] or not t_userData.tasks[s_type][n_subTaskId] then
		bneed_init = true
	end

	if bneed_init then
		local bsuc = init_mission(t_userData, s_type)
		if not bsuc then
			return
		end
	end

	local t_task = t_userData.tasks[s_type][n_subTaskId]
	local t_taskConf 		= uu.table_value_copy(mis_data.t_share_mission[s_type])
	if not(t_taskConf and t_taskConf.t_conf and t_taskConf.t_conf.t_mission) then
		return
	end

	-- 获取子任务配置
	local t_mission
	local t_temp = t_taskConf.t_conf

	for _ , v in next, t_temp.t_mission do
		for _, v2 in next , v do
			if v2.n_subTaskId == n_subTaskId then
				t_mission = v2
				break
			end
		end
	end
	if not t_mission then
		return
	end
	local b_update = false
	-- 刷新任务
	if t_mission.b_daily and mis_common.is_another_day(t_task.refreshTime, n_nowtime) then
		b_update = true
	end
	if t_mission.b_weekly and mis_common.is_another_week(t_task.refreshTime, n_nowtime) then
		b_update = true
	end

	if b_update then
		local n_mode = t_taskConf.t_conf.b_auto and 1 or 0
		t_task.progress = 0
		t_task.status = 0
		t_task.refreshTime = n_nowtime
		t_task.rewardTime = 0
		t_task.mode = n_mode
	end

	return t_userData.tasks[s_type][n_subTaskId]
end


local get_prize_data = function(prize_result, totalScore, prize_type, task_prize_conf, prize_lst)
	if not task_prize_conf[prize_type] then
		return t_retCode.n_NO_DATA, 'vip_theme custom prize config is nil!'
	end

	local add_state = 0
	for stageid, data in next, task_prize_conf[prize_type] do
		if totalScore >= data.score then
			local prize_flag = lshift(1, stageid-1)
			local get_flag = band(prize_result, prize_flag)
			if get_flag == 0 then  --还未领取过
				add_state = add_state + prize_flag
				for _, iteminfo in next, data.item do
					prize_lst[iteminfo.id] = prize_lst[iteminfo.id] or 0
					prize_lst[iteminfo.id] = prize_lst[iteminfo.id] + iteminfo.num 
				end
			end
		end
	end
	return add_state
end


-- !@ 返回给客户端数据
local get_vip_theme_respose = function(uin, s_type)
	local t_vip_theme_Conf 	= mis_data.t_share_mission.vip_theme.t_conf
	local vip_buy_cfg = mis_data.VipCardBuy
	if not t_vip_theme_Conf or not t_vip_theme_Conf.vip_theme or not vip_buy_cfg then
		return {}
	end
	local tcustom_theme = t_vip_theme_Conf.vip_theme
	local discount = tcustom_theme.discount
	local t_user = mis_data.get_user(uin)
	local n_code, t_userData = t_user:get_share_mission_data(s_type)
	if n_code ~= t_retCode.n_OK  then
		return {}
	end

	local tresp = {}
	tresp.pay_lst = t_userData.pay_lst
	tresp.joined = t_userData.vip_buy and t_userData.vip_buy and 1 or 0
	tresp.buy_cfg = {[1] = vip_buy_cfg[tcustom_theme.friend_storeid ].cost_num }
	for count = 2, 5 do
		tresp.buy_cfg[count] = math_ceil(count * discount[count].rate * vip_buy_cfg[tcustom_theme.group_storeid].cost_num)
	end
	tresp.upgrade_price = tcustom_theme.upgrade_price  --奖励升级价格
	tresp.friend_times_max  = tcustom_theme.friend_times  -- 友谊之光次数
	tresp.friend_times_cur =  t_userData.friend_buy and #t_userData.friend_buy or 0
	tresp.update_prize = t_userData.update_prize or 0  --奖励进阶标识
	tresp.prize_result_1 = t_userData.prize_result_1 or 0 -- 普通奖励领取结果
	tresp.prize_result_2 = t_userData.prize_result_2 or 0 -- 进阶奖励领取结果
	tresp.task_max_times = tcustom_theme.task_max_times[1]  -- 任务最大完成次数
	tresp.task_score = get_task_score()
	tresp.task_data = {}
	for sub_taskid, _ in next, tcustom_theme.task_max_times[1] do 
		local t_task = get_task_data(t_userData, s_type, sub_taskid)
		if t_task then
			tresp.task_data[sub_taskid] = { progress = t_task.progress, status = t_task.status, event = t_task.event, total = t_task.total }
		end
	end

	local prize_lst = {}
	local add_flag_1 = get_prize_data(t_userData.prize_result_1 or 0, tresp.task_score , 1, tcustom_theme.prize, prize_lst)
	local add_flag_2 = 0
	if t_userData.update_prize then
		add_flag_2 = get_prize_data(t_userData.prize_result_2 or 0, tresp.task_score , 2, tcustom_theme.prize, prize_lst)
	end
	tresp.has_prize = add_flag_1 + add_flag_2 > 0 and 1 or 0
	return tresp
end

-- !@发送团购邮件
local send_group_mail = function( t_uin_lst, t_uin_name, config)
	local mail_conf = config.group_mail
	local mem_num = #t_uin_lst
	local discount_conf = config.discount[mem_num]
	if not discount_conf then
		log.day_list("vip_theme", "has not discount info! t_uin_lst="..mis_uu.to_str(t_uin_lst) .." , mem_num=" .. mem_num)
		return
	end
	for _idx, p_uin in next, t_uin_lst do
		local mem_uin = tonumber(p_uin)
		local count = 0
		local name_str = ""
		for _idx_name, s_name in next, t_uin_name do
			if _idx ~= _idx_name then
				if count > 0 then
					name_str = name_str .. "、"
				end

				name_str = name_str .. s_name
				count = count + 1
			end
		end
		local s_boby = string_format(mail_conf.body,  name_str )
		-- 赠送邮件
		local n_code, ts_desc = mis_bridge.send_email(mem_uin, ngx.escape_uri(mail_conf.title) , ngx.escape_uri(s_boby), OVERDUE_SEC, discount_conf.item )
		if n_code ~= t_retCode.n_OK then
			log.day_list("vip_theme", "send_group_mail failed! uin="..mem_uin .." ,ts_desc=" .. (ts_desc or ''))
		end
	end
end


-- !@判断是否可以发起团购
local vip_batchbuy_can_buy = function(uin, s_type, t_uin_lst)
	local t_payuser = mis_data.get_user(uin)
	local n_code_p, t_userData_p = t_payuser:get_share_mission_data(s_type)
	if n_code_p ~= t_retCode.n_OK  then
		return n_code_p, 'get user data error'
	end

	local t_all_lst = {}
	t_all_lst[uin] = 1
	-- if t_userData_p.pay_lst then
	-- 	for _, s_uin in next, t_userData_p.pay_lst do
	-- 		local n_uin = tonumber(s_uin)
	-- 		t_all_lst[n_uin] = 1
	-- 	end
	-- end

	local bcando = true
	local tresult = {}
	local trecord = {}
	for _, s_uin in next, t_uin_lst do
		local n_uin = tonumber(s_uin)
		-- if not t_all_lst[n_uin] then
		-- 	log.day_list("vip_theme", "has unknown uin in the list",n_uin, mis_uu.to_str(ngx.ctx.t_params))
		-- 	bcando = false
		-- 	tresult[n_uin] = t_retCode.n_OPER_ERR
		-- end

		if trecord[n_uin] then
			log.day_list("vip_theme", "has repeat uin in the list",n_uin, mis_uu.to_str(ngx.ctx.t_params))
			bcando = false
			tresult[n_uin] = t_retCode.n_OPER_ERR
		end
		trecord[n_uin] = 1
		t_all_lst[n_uin] = nil
		local t_user = mis_data.get_user(n_uin)
		local n_code, t_userData = t_user:get_share_mission_data(s_type)
		if t_userData.vip_buy then
			bcando = false
			tresult[n_uin] = t_retCode.n_HAS_VIP_BUY
		end
	end

	if not bcando then
		return t_retCode.BATCH_BUY_FAILED, tresult
	end

	local count = 0
	for _uin, _ in next, t_all_lst do
		count = count + 1
	end
	if count > 0 then
		log.day_list("vip_theme", "has repeat uin in the list", mis_uu.to_str(ngx.ctx.t_params))
		return t_retCode.n_OPER_ERR, 'group data is not match!'
	end
	return t_retCode.n_OK
end

-- 获得拼团数据
this.batchbuy_vip_get_data = function()
	local uin = ngx.ctx.t_params.uin
	local s_type = mis_config.t_workType.s_VIP_THEME
	local t_payuser = mis_data.get_user(uin)
	local n_code_p, t_userData = t_payuser:get_share_mission_data(s_type)
	if n_code_p ~= t_retCode.n_OK then
		return n_code_p, 'get user data error'
	end
	-- 触发登录任务
	local n_code, s_desc, bupdate = this.share_missionEventCore(t_userData, s_type, 18, 1)
	if n_code ~= t_retCode.n_OK and n_code ~= t_retCode.n_NO_EVENT_TYPE then
		log.day_list("vip_theme","share_mission_event login error|" .. t_payuser.n_uin .. '|' .. uu.to_str(n_code) .. '|' .. s_type .. '|' .. s_desc)
	end
	if bupdate then
		t_payuser:set_share_mission_data(s_type, t_userData)
	end

	return  t_retCode.n_OK, get_vip_theme_respose(uin, s_type)
end

-- 添加vip团购成员
this.vip_batchbuy_update_mem = function()
	local uin = ngx.ctx.t_params.uin
	local op_uin = ngx.ctx.t_params.op_uin
	local idx    = ngx.ctx.t_params.index
	local s_type = mis_config.t_workType.s_VIP_THEME

	local t_payuser = mis_data.get_user(uin)
	local n_code_p, t_userData_p = t_payuser:get_share_mission_data(s_type)
	if n_code_p ~= t_retCode.n_OK then
		return n_code_p, 'get user data error'
	end

	if op_uin == uin then  -- 不能添加自己
		return t_retCode.n_OPER_ERR, op_uin
	end

	if op_uin > 0 then
		local t_user = mis_data.get_user(op_uin)
		local n_code, t_userData = t_user:get_share_mission_data(s_type)
		if n_code ~= t_retCode.n_OK then
			return n_code, 'get user data error'
		end

		if t_userData.vip_buy then
			return t_retCode.n_HAS_VIP_BUY, op_uin
		end

		-- 检查玩家是否在团购列表中
		if t_userData_p.pay_lst then
			for _idx, _uin	in next, t_userData_p.pay_lst do
				if _uin == op_uin then  --已经在列表里
					if _idx ~= idx then
						return t_retCode.n_OPER_ERR, op_uin
					else
						return t_retCode.n_OK, t_userData_p.pay_lst
					end
				end
			end
		end
	end

	local tdata = mis_bridge.query_user_vip_condition(uin, op_uin, 1001)
	if tdata == nil then
		return t_retCode.n_SYS_ERR, 'check condition error'
	end

	if tdata.ret ~= t_retCode.n_OK then
		return  t_retCode.n_VIP_LIMIT_MAX, op_uin
	end

	t_userData_p.pay_lst = t_userData_p.pay_lst or {}
	t_userData_p.pay_lst[idx] = op_uin
	t_payuser:set_share_mission_data(s_type, t_userData_p)

	return  t_retCode.n_OK, get_vip_theme_respose(uin, s_type)
end


-- !@vip团购
this.vip_batchbuy_req = function()
	local uin = ngx.ctx.t_params.uin
	local uin_lst = ngx.ctx.t_params.uin_lst
	local name_lst = ngx.ctx.t_params.name_lst
	local s_type = mis_config.t_workType.s_VIP_THEME
	local t_vip_theme_Conf 	= mis_data.t_share_mission.vip_theme.t_conf
	if t_vip_theme_Conf == nil then
		return t_retCode.n_NO_DATA, 'vip_theme config is nil!'
	end

	if t_vip_theme_Conf.vip_theme == nil then
		return t_retCode.n_NO_DATA, 'vip_theme custom config is nil!'
	end
	local store_id = t_vip_theme_Conf.vip_theme.group_storeid
	local t_uin_lst = mis_uu.split(uin_lst, '_')
	local t_tmp_name = mis_uu.split(name_lst, '_')
	local t_uin_name = {}
	for _, name in next, t_tmp_name do
		table_insert(t_uin_name, ngx.unescape_uri(name))
	end
	local ncode, reason = vip_batchbuy_can_buy(uin, s_type, t_uin_lst)
	if ncode  ~= t_retCode.n_OK then
		return ncode, reason
	end

	local count = #t_uin_lst
	local discount = t_vip_theme_Conf.vip_theme.discount[count].rate
	if not discount then
		log.day_list("vip_theme", "vip_batchbuy_req discount is nil! count =", count)
		return t_retCode.n_SYS_ERR, 'discount is nil!'
	end

	local response = mis_bridge.vip_batchbuy_req(uin, uin_lst, store_id, discount)
	if not response then
		log.day_list("vip_theme", "vip_batchbuy_req failed =", mis_uu.to_str(ngx.ctx.t_params))
		return t_retCode.n_SYS_ERR, 'buy vip error'
	end

	if response.ret ~= t_retCode.n_OK then
		log.day_list("vip_theme", "vip_batchbuy_req failed =", mis_uu.to_str(ngx.ctx.t_params),  mis_uu.to_str(response))
		return response.ret, response.data
	end

	-- 购买成功,则设置所有迷你号的团购标记
	for _, s_uin in next, t_uin_lst do
		local n_uin = tonumber(s_uin)
		local t_user = mis_data.get_user(n_uin)
		local n_code, t_userData = t_user:get_share_mission_data(s_type)
		if n_code == t_retCode.n_OK then
			t_userData.vip_buy = 1 --标记已参加团购
			t_user:set_share_mission_data(s_type, t_userData)
		end
	end
	send_group_mail( t_uin_lst, t_uin_name, t_vip_theme_Conf.vip_theme)
	return t_retCode.n_OK, get_vip_theme_respose(uin, s_type)
end


-- !@友谊之光
this.vip_friendbuy_req = function()
	local uin = ngx.ctx.t_params.uin
	local friend_uin = ngx.ctx.t_params.friend_uin
	local uinname = ngx.unescape_uri(ngx.ctx.t_params.uinname or '')
	local friendname = ngx.unescape_uri(ngx.ctx.t_params.friendname or '')
	local bless = ngx.unescape_uri(ngx.ctx.t_params.bless or '')
	local s_type = mis_config.t_workType.s_VIP_THEME
	local t_vip_theme_Conf 	= mis_data.t_share_mission.vip_theme.t_conf
	if t_vip_theme_Conf == nil then
		return t_retCode.n_NO_DATA, 'vip_theme config is nil!'
	end

	if t_vip_theme_Conf.vip_theme == nil then
		return t_retCode.n_NO_DATA, 'vip_theme custom config is nil!'
	end
	local store_id = t_vip_theme_Conf.vip_theme.friend_storeid
	local t_payuser = mis_data.get_user(uin)
	local n_code_p, t_userData_p = t_payuser:get_share_mission_data(s_type)
	if n_code_p ~= t_retCode.n_OK  then
		return n_code_p, 'get user data error'
	end

	if t_userData_p.friend_buy and #t_userData_p.friend_buy >= t_vip_theme_Conf.vip_theme.friend_times then
		return t_retCode.n_VIP_TIMES_USED, 'max times!'
	end

	local response = mis_bridge.vip_friendbuy_req(uin, friend_uin, store_id)
	if not response then
		log.day_list("vip_theme", "vip_friendbuy_req failed =", mis_uu.to_str(ngx.ctx.t_params))
		return t_retCode.n_SYS_ERR, 'buy vip error'
	end

	if response.ret ~= t_retCode.n_OK then
		log.day_list("vip_theme", "vip_friendbuy_req failed =", mis_uu.to_str(ngx.ctx.t_params),  mis_uu.to_str(response)) 
		return response.ret, response.data
	end

	t_userData_p.friend_buy = t_userData_p.friend_buy or {}
	table_insert(t_userData_p.friend_buy, friend_uin)
	local n_code, s_desc = t_payuser:set_share_mission_data(s_type, t_userData_p)

	if n_code ~= t_retCode.n_OK  then
		return n_code, s_desc
	end

	-- 发送邮件给赠送方
	local present_mail = t_vip_theme_Conf.vip_theme.friend_mail
	local uin_str = friendname.."("..friend_uin..")"
	local s_boby = string_format(present_mail.body,  uin_str )
	n_code, s_desc = mis_bridge.send_email(uin, ngx.escape_uri(present_mail.title), ngx.escape_uri(s_boby), OVERDUE_SEC)
	if n_code ~= t_retCode.n_OK then
		log.day_list("vip_theme", "send_prenset_mail failed! uin="..uin .." ,ts_desc=" .. (s_desc or ''))
	end

	local get_mail = t_vip_theme_Conf.vip_theme.friend_mail_get
	local friend_str = uinname.."("..uin..")"
	s_boby = string_format(get_mail.body,  friend_str,  bless)
	n_code, s_desc = mis_bridge.send_email(friend_uin, ngx.escape_uri(get_mail.title), ngx.escape_uri(s_boby), OVERDUE_SEC)
	if n_code ~= t_retCode.n_OK then
		log.day_list("vip_theme", "send_get_mail failed! friend_uin="..friend_uin .." ,ts_desc=" .. (s_desc or ''))
	end

	--local tresult = {uin, friend_uin}
	return t_retCode.n_OK,  get_vip_theme_respose(uin, s_type)
end

--------------------------------------------------------------
-- 说明：任务奖励升阶
-- 返回：n					状态码
--		ts 					描述
this.task_prize_upgrade = function()
	local uin = ngx.ctx.t_params.uin
	local s_type = mis_config.t_workType.s_VIP_THEME
	local t_vip_theme_Conf 	= mis_data.t_share_mission.vip_theme.t_conf
	if not t_vip_theme_Conf then
		return t_retCode.n_NO_DATA, 'vip_theme config is nil!'
	end

	if not t_vip_theme_Conf.vip_theme then
		return t_retCode.n_NO_DATA, 'vip_theme custom config is nil!'
	end

	local t_payuser = mis_data.get_user(uin)
	local n_code_p, t_userData_p = t_payuser:get_share_mission_data(s_type)
	if n_code_p ~= t_retCode.n_OK  then
		return n_code_p, 'get user data error'
	end

	if t_userData_p.update_prize then
		return t_retCode.n_WORK_ERR, 'has updated the task prize!'
	end

	local ncost_num = t_vip_theme_Conf.vip_theme.upgrade_price
	if ncost_num == nil or ncost_num < 0 then
		return t_retCode.n_NO_DATA, 'vip_theme custom config error!'
	end
	-- 扣迷你币
	local n_code, s_desc = mis_bridge.cost_money(uin, nil, ncost_num, mis_config.t_costType.n_COIN, 'vip_theme_task',
	     mis_common.table_to_json({aname = 'vip_theme_task', now = mis_uu.now()}))

	if n_code ~= t_retCode.n_OK then
		log.day_list("vip_theme", "task_prize_upgrade", "cost coin error", uin, n_code, uu.to_str(s_desc), uu.to_str(t_userData_p))
		return n_code, s_desc
	end

	t_userData_p.update_prize = 1
	 n_code, s_desc = t_payuser:set_share_mission_data(s_type, t_userData_p)
	if n_code ~= t_retCode.n_OK then
		return n_code_p, s_desc or 'save data failed!'
	end

	return t_retCode.n_OK, get_vip_theme_respose(uin, s_type)
end


--------------------------------------------------------------
-- 说明：任务奖励领取
-- 返回：n					状态码
--		ts 					描述
this.task_prize_get_reward = function()
	local uin = ngx.ctx.t_params.uin
	local s_type = mis_config.t_workType.s_VIP_THEME
	local t_vip_theme_Conf 	= mis_data.t_share_mission.vip_theme.t_conf
	if not t_vip_theme_Conf then
		return t_retCode.n_NO_DATA, 'vip_theme config is nil!'
	end

	if not t_vip_theme_Conf.vip_theme == nil or not t_vip_theme_Conf.vip_theme.prize then
		return t_retCode.n_NO_DATA, 'vip_theme custom config is nil!'
	end

	local t_payuser = mis_data.get_user(uin)
	local n_code_p, t_userData_p = t_payuser:get_share_mission_data(s_type)
	if n_code_p ~= t_retCode.n_OK  then
		return n_code_p, 'get user data error'
	end

	local task_prize_conf = t_vip_theme_Conf.vip_theme.prize
	local prize_lst = {}
	local totalScore = get_task_score(uin)
	-- 获得普通可领取奖励
	local add_flag_1 = get_prize_data(t_userData_p.prize_result_1 or 0, totalScore , 1, task_prize_conf, prize_lst)
	-- 获得进阶奖励
	local add_flag_2 = 0
	if t_userData_p.update_prize then
		add_flag_2 = get_prize_data(t_userData_p.prize_result_2 or 0, totalScore , 2, task_prize_conf, prize_lst)
	end

	local item_lst = {}
	for item_id, item_num in next, prize_lst do
		table_insert(item_lst, {id = item_id, num = item_num})
	end

	-- 获得普通可领取奖励
	if add_flag_1 + add_flag_2 == 0 then
		return t_retCode.n_OK, get_vip_theme_respose(uin, s_type)
	end

	t_userData_p.prize_result_1 = t_userData_p.prize_result_1 or 0
	t_userData_p.prize_result_1 = t_userData_p.prize_result_1 + add_flag_1
	t_userData_p.prize_result_2 = t_userData_p.prize_result_2 or 0
	t_userData_p.prize_result_2 = t_userData_p.prize_result_2 + add_flag_2
	local n_code, s_desc = t_payuser:set_share_mission_data(s_type, t_userData_p)
	if n_code ~= t_retCode.n_OK then
		return n_code_p, s_desc or 'save data failed!'
	end

	-- -- 添加奖励
	local n_code_i, s_desc_i, s_items = mis_data.add_items(uin, item_lst, mis_config.t_workType.s_VIP_THEME, 'vip_theme_taskPrize')
	if n_code_i ~= t_retCode.n_OK then
		log.day_list('vip_theme', 'task_prize_get_reward|' .. uin .. '|' .. s_items)
		return n_code, s_desc_i
	end

	local response = get_vip_theme_respose(uin, s_type)
	response.prize_lst = item_lst
	return t_retCode.n_OK, response
end 
--------------------------------------------------------------
-- 说明：vip_theme 业务前置检测
-- 返回：n					状态码
--		ts 					描述
this.pre_vip_theme = function()
	if mis_data.t_share_mission.vip_theme.n_status ~= mis_config.t_workStatus.n_ENABLE then
		return t_retCode.n_OPER_ERR, 'vip_theme disable'
	end

	local t_params  		= ngx.ctx.t_params
	local t_vip_theme_Conf 	= mis_data.t_share_mission.vip_theme.t_conf
	-- 活动有效期间
	local n_nowtime 	= mis_data.get_server_time()
	--local n_valid 		= t_vip_theme_Conf.onshow or 0
	local n_start_time 	= t_vip_theme_Conf.n_startTime
	local n_end_time   	= t_vip_theme_Conf.n_endTime
	if n_nowtime < n_start_time or n_nowtime > n_end_time then
		return t_retCode.n_OPER_ERR, 'invalid activity'
	end

	-- 渠道控制
	-- if t_vip_theme_Conf.t_apiids then
	-- 	local b_valid_apiid  = false
	-- 	local n_apiid  =  tonumber(t_params.apiid or 0)
	-- 	local t_apiids = t_vip_theme_Conf.t_apiids
	-- 	for _, v in pairs(t_apiids) do
	-- 		if v and tonumber(v) == n_apiid then
	-- 			b_valid_apiid = true
	-- 			break
	-- 		end
	-- 	end
	-- 	if not b_valid_apiid then
	-- 		return t_retCode.n_OPER_ERR, 'apiid is invalid'
	-- 	end
	-- end

	-- 版本号控制
	if t_vip_theme_Conf.n_version_min then
		local n_l = mis_common.version_to_number(t_params.ver or 0)
		local n_r = mis_common.version_to_number(t_vip_theme_Conf.n_version_min or 0)
		if n_l < n_r then
			return t_retCode.n_OPER_ERR, 'version too low'
		end
	end

	 return t_retCode.n_OK
end


local share_mission_event_handle = function( t_userData, s_type, t_mission, n_event, n_value, task_max_times)
	local n_subTaskId = t_mission.n_subTaskId
	local t_task = get_task_data(t_userData,s_type, n_subTaskId )
	if not t_task then
		return t_retCode.n_NO_DATA, 'can not find task!'
	end

	local t_user = mis_data.get_user()
	if task_max_times and t_task.total >= task_max_times then
		return t_retCode.n_OK,  'finish full!'
	end

	local s_logType			= s_type .. '_record'
	local n_nowTime 		= mis_data.get_server_time()
	local t_params 			= ngx.ctx.t_params
	local n_target = t_mission.n_target
	local t_autoAward = {share = {}, items = {}}
	local finish_times = 0
	local budpate = false
	-- 更新进度
	if t_task.status == 0 then
		budpate = true
		local bmulti_times =false  --多次完成标识
		-- 获得指定道具类型的任务
		t_task.progress = t_task.progress + n_value
		t_task.refreshTime 	= n_nowTime
		if n_subTaskId == 4 then  -- 消耗迷你豆任务是循环任务
			if t_task.progress >= n_target then
				bmulti_times = true
			end
		end

		while (t_task.progress >= n_target)  -- 处理循环任务
		do
			t_task.progress = t_task.progress - n_target
			t_task.status = 1
			t_task.total = t_task.total + 1
			t_task.refreshTime 	= n_nowTime
			finish_times = finish_times + 1
			t_task.rewardTime = n_nowTime
			-- 自动领取奖励
			if t_task.mode == 1 and t_task.status == 1 and t_mission.t_bonus and mis_uu.tableNotEmpty(t_mission.t_bonus) then
				mis_extend.share_mission_getaward_core(t_userData, s_type, n_subTaskId, 1, t_mission, t_autoAward)
			end
			if not bmulti_times or task_max_times and t_task.total >= task_max_times then
				t_task.progress = n_target
				break
			end
		end
	end

	if #t_autoAward.items > 0 then
		local n_code, s_desc = mis_data.add_items(t_user.n_uin, t_autoAward.items, s_type, 'auto_award')
		if n_code ~= t_retCode.n_OK then
			log.day_list("share_mission_error","share_mission_event_handle add_items error|" .. t_user.n_uin .. '|' .. s_type .. '|' .. uu.to_str(n_code) .. '|' .. uu.to_str(s_desc) .. '|' .. uu.to_str(t_autoAward))
			return n_code, s_desc
		end
	end

	-- 任务完成
	if finish_times > 0 then
		if task_max_times and t_task.total < task_max_times then  -- 3,4号任务是循环任务,完成一次要初始化
			if n_subTaskId == 3 then
				t_task.progress = 0
				t_task.status = 0
				t_task.rewardTime = 0
			elseif  n_subTaskId == 4 then
				t_task.status = 0
				t_task.rewardTime = 0
			end
		end

		--埋点数据
		mis_log.remote_pure_day(s_logType, mis_common.table_to_json({
			key = 'finish';
			finish_times = finish_times;
			total  = t_task.total or 0;
			ip = t_params.s_remoteAddr or ngx.ctx.xff or ngx.var.remote_addr or "nil";
			uin = t_params.uin;
			time = n_nowTime;
			country = t_params.country or 'nil';
			apiid = t_params.apiid or 'nil';
			ver = t_params.ver or 'nil';
			lang = t_params.lang or 'nil';
			type = s_type;
			event = n_event;
			taskid = n_subTaskId;
			value = n_value;
		}))
	end
	return t_retCode.n_OK,  'task update ok!', budpate
end

-- 会员主题活动事件触发核心逻辑
 this.share_missionEventCore = function( t_userData, s_type, n_event, n_value)
	local t_vip_theme_Conf 	= mis_data.t_share_mission.vip_theme.t_conf
	if t_vip_theme_Conf == nil then
		return t_retCode.n_NO_DATA, 'vip_theme config is nil!'
	end

	local t_taskConf 		= uu.table_value_copy(mis_data.t_share_mission[s_type])
	if not(t_taskConf and t_taskConf.t_conf and t_taskConf.t_conf.t_mission) then
		return t_retCode.n_WORK_ERR, 'The mission is not exist!'
	end

	-- 获取子任务配置
	local t_mission
	local t_temp = t_taskConf.t_conf
	for k, v in next, t_temp.t_mission do
		for _, v2 in next, v do
			if v2.n_eventId == n_event then
				t_mission = v2
				break
			end
		end
	end

	if not t_mission then
		return t_retCode.n_OK,  'not find task!'
	end

	local taskdata = get_task_data(t_userData, s_type, t_mission.n_subTaskId)
	if not taskdata then
		return  t_retCode.n_NO_DATA, 'vip_theme taskdata is nil!'
	end

	local max_times_conf = t_vip_theme_Conf.vip_theme.task_max_times
	local max_times = max_times_conf[1][t_mission.n_subTaskId]
	return share_mission_event_handle( t_userData, s_type, t_mission, n_event, n_value, max_times)
end



-- 清除玩家的任务数据
this.clear_mission_data = function()
	local uin = ngx.ctx.t_params.uin
	local s_type = mis_config.t_workType.s_VIP_THEME
	local t_user = mis_data.get_user(uin)
	local n_spcode, t_spData = t_user:get_specialItem_data()
	if n_spcode ~= t_retCode.n_OK then
		return 0
	end
	t_spData[mis_config.t_specialItem.s_THEMESCORE] = nil
	t_user:set_specialItem_data(t_spData)

	local n_code, s_desc = t_user:set_share_mission_data(s_type, {})
	n_spcode, t_spData = t_user:get_specialItem_data()
	local _, t_userData = t_user:get_share_mission_data(s_type)
	return n_code, { t_userData = t_userData,  t_spData = t_spData}
end
return this
